//
//  LeftCollectionViewCell.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/27.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit

protocol LeftCollectionViewCellDelegate {
    func letSelfGotoProductVCWithStr(_ id:String,name:String)
}

class LeftCollectionViewCell: UICollectionViewCell,UICollectionViewDelegate,UICollectionViewDataSource {
    
    var delegate: LeftCollectionViewCellDelegate?
    var myCollectionView : UICollectionView?
    
    let SECONDCELL = "secondCell"
    let NONECELL = "noneCell"
    
    var myDataArr = NSArray()
    
    
    var dataArray : NSArray?{
        didSet{
            myDataArr = dataArray!
            self.creatCollectionView()
            self.myCollectionView?.reloadData()
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH*0.75, height: SCREEN_HEIGHT*0.95-64))
        
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension LeftCollectionViewCell{
    fileprivate func creatCollectionView(){
        let layout  = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (SCREEN_WIDTH*0.75-20)/3, height: (SCREEN_WIDTH*0.75-20)/3*1.4)
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 5
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 0, 5)
        layout.scrollDirection = .vertical
        
        let noneLayout = UICollectionViewFlowLayout()
        noneLayout.itemSize = CGSize(width: SCREEN_WIDTH*0.75, height: SCREEN_HEIGHT*0.95-64)
        
        
        if myDataArr.count == 0 {
            myCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH*0.75, height: SCREEN_HEIGHT*0.95-64), collectionViewLayout: noneLayout)
        }else{
            myCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH*0.75, height: SCREEN_HEIGHT*0.95-64), collectionViewLayout: layout)
        }
        
        myCollectionView?.backgroundColor = UIColor.white
//        myCollectionView?.showsVerticalScrollIndicator = true
//        myCollectionView?.showsHorizontalScrollIndicator = false
        myCollectionView?.delegate = self
        myCollectionView?.dataSource = self
        myCollectionView?.alwaysBounceVertical = true
        self.contentView.addSubview(myCollectionView!)
        
        let nib = UINib(nibName: "SecondLeftCollectionViewCell", bundle: nil)
        myCollectionView?.register(nib, forCellWithReuseIdentifier: SECONDCELL)
        
        myCollectionView?.register(NoneCollectionViewCell.self, forCellWithReuseIdentifier: NONECELL)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if self.myDataArr.count == 0 {
            return 1
        }
        return self.myDataArr.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SECONDCELL, for: indexPath)
        
        if myDataArr.count == 0 {
            let noneCell = collectionView.dequeueReusableCell(withReuseIdentifier: NONECELL, for: indexPath)
            
            return noneCell
        }
        let model = self.myDataArr[indexPath.item]as?LeftModel
        (cell as? SecondLeftCollectionViewCell)?.model = model
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if self.myDataArr.count > 0 {
            let model = self.myDataArr[indexPath.item] as? LeftModel
            
            self.delegate?.letSelfGotoProductVCWithStr((model?.id)!, name: (model?.categories_name)!)
        }
        
        
    }
    
}
















